﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace WenSkin.DLL
{
    public static class FileFolderHelper
    {
        #region 目录文件获取类

        /// <summary>
        /// 获取及其子目录下全部文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<string> GetFiles(this string path)
        {
            List<string> files = new List<string>();

            //获得程序所在路径的目录对象
            DirectoryInfo dirs = new DirectoryInfo(path);

            //获得目录下文件夹对象
            foreach (var dir in dirs.GetDirectories())
            {
                files.AddRange(GetFiles(dir.FullName));
            }

            //获得目录下文件对象
            foreach (var file in dirs.GetFiles())
            {
                files.Add(file.FullName);
            }
            return files;
        }

        /// <summary>
        /// 返回当前目录及子目录的文件列表。
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<FileInfo> GetFileInfos(this string path)
        {
            List<FileInfo> files = new List<FileInfo>();

            //获得程序所在路径的目录对象
            DirectoryInfo dirs = new DirectoryInfo(path);

            //获得目录下文件夹对象
            foreach (var dir in dirs.GetDirectories())
            {
                files.AddRange(GetFileInfos(dir.FullName));
            }

            //获得目录下文件对象
            files.AddRange(dirs.GetFiles());
            return files;
        }        
        
        /// <summary>
        /// 返回当前目录的文件列表。
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<FileInfo> GetFolderFileInfos(this string path)
        {
            List<FileInfo> files = new List<FileInfo>();
            //获得程序所在路径的目录对象
            DirectoryInfo dirs = new DirectoryInfo(path);
            //获得目录下文件对象
            files.AddRange(dirs.GetFiles());
            return files;
        }

        /// <summary>
        /// 获取目录下的全部文件不包含子文件夹
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<string> GetFolderFiles(this string path)
        {
            List<string> files = new List<string>();

            //获得程序所在路径的目录对象
            DirectoryInfo dirs = new DirectoryInfo(path);

            //获得目录下文件对象
            foreach (var file in dirs.GetFiles())
            {
                files.Add(file.FullName);
            }
            return files;
        }

        #endregion

        #region 运行文件类


        /// <summary>
        /// 打开指定文件
        /// </summary>
        /// <param name="path"></param>
        public static void FileOpen(this string path)
        {
            Process pr = new Process();//声明一个进程类对象
            pr.StartInfo.FileName = path;
            pr.Start();
        }

        /// <summary>
        /// 打开文件带参数
        /// </summary>
        /// <param name="path"></param>
        /// <param name="arg"></param>
        public static void FileOpen(this string path, string arg)
        {
            Process pr = new Process();//声明一个进程类对象
            pr.StartInfo.FileName = path;
            pr.StartInfo.Arguments = arg;
            pr.Start();
        }

        #endregion

        #region 文件定位

        public static void OpenFolderAndSelectFile(string fileFullName)
        {
            ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
            psi.Arguments = "/e,/select," + fileFullName;
            Process.Start(psi);
        }

        #endregion

        #region 文件复制类

        /// <summary>
        /// 复制整个文件夹到指定文件夹
        /// </summary>
        /// <param name="dir">本地文件夹</param>
        /// <param name="dirCopy">目标文件夹</param>
        public static void DirectoryFileCopy(string dir, string dirCopy)
        {
            if (!Directory.Exists(dirCopy))
            {
                Directory.CreateDirectory(dirCopy);
            }

            //获得程序所在路径的目录对象
            DirectoryInfo dirs = new DirectoryInfo(dir);

            foreach (var d in dirs.GetDirectories())
            {
                DirectoryFileCopy(d.FullName, dirCopy + "\\" + d.Name);
            }

            foreach (var file in dirs.GetFiles())
            {
                string copyPath = dirCopy + "\\" + file.Name;
                file.CopyTo(copyPath, true);
            }
        }


        /// <summary>
        /// 复制集合文件都指定目录，存在替换
        /// </summary>
        /// <param name="files"></param>
        /// <param name="dirCopy"></param>
        public static void DirectoryFileScreenCopy(List<string> files, string dirCopy)
        {
            foreach (var file in files)
            {
                string copyPath = dirCopy + "\\" + Path.GetFileName(file);
                File.Copy(file, copyPath, true);
            }
        }

        #endregion

        #region 筛选文件类

        /// <summary>
        /// 筛选满足条件的文件
        /// </summary>
        /// <param name="files"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public static List<string> DirectoryFileScreen(List<string> files, string where)
        {
            return files.FindAll(q => q.Contains(where));
        }

        #endregion

        #region 文件内容操作类

        /// <summary>
        /// 读取路径下的文件内容
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ReadText(string path)
        {
            return ReadAllText(path, Encoding.Default);
        }
        /// <summary>
        /// 读取路径下的文件内容
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ReadAllText(string path, Encoding encoding)
        {
            return File.ReadAllText(path, encoding);
        }

        /// <summary>
        /// 在指定路径中创建所有目录和子目录，除非它们已经存在。
        /// 打开一个文件，向其中追加指定的字符串，然后关闭该文件。 如果文件不存在，此方法将创建一个文件，将指定的字符串写入文件，然后关闭该文件。
        /// </summary>
        /// <param name="path"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string FileAppendAllText(string path, string text)
        {
            Directory.CreateDirectory(Path.GetDirectoryName(path));
            File.AppendAllText(path, text);
            return File.ReadAllText(path);
        }

        /// <summary>
        /// 在指定路径中创建所有目录和子目录，除非它们已经存在。
        /// 创建一个新文件，向其中写入指定的字符串，然后关闭文件。 如果目标文件已存在，则覆盖该文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string FileWriteAllText(string path, string text)
        {
            Directory.CreateDirectory(Path.GetDirectoryName(path));
            File.WriteAllText(path, text);
            return text;
        }

        #endregion
    }
}
